"use strict";
let getIP = function() {
	let ip = null;
	let interfaces = require("os").networkInterfaces();
	let isPrivateIp = function(address) {
		if (address.startsWith("10.") || address.startsWith("192.168")) {
			console.log("私有ip类型a，c", address);
			ip = address;
			return true;
		}
		let nets = address.split(".");
		if (nets[0] == 172 && (nets[1] >= 16 && nets[1] <= 31)) {
			console.log("私有ip类型b", address);
			ip = address;
			return true;
		}
		console.log("第一个公有ip地址");
		return false;
	};
	let findIpFromInterface = function(intf) {
		if (intf.family === "IPv4" && !intf.internal && !isPrivateIp(intf.address)) {
			ip = intf.address;
			return true;
		} else {
			return false;
		}
	};
	let result = Object.keys(interfaces).some(key => {
		return interfaces[key].some(findIpFromInterface);
	});
	console.log("本机ip是", ip);
	if (!result) {
		console.error("没有自动找到ip，将使用内网ip代替，如果有需要请手动修改配置文件");
	}
	return ip;
};
module.exports = {
	getIP
};